﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web.Security;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Tesla.Service.DataContracts;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using System.ServiceModel;
using Tesla.Service.WcfServiceCommon;
//using Telerik.Web.UI;
using Tesla.Util.Utilities;
using System.IO;

namespace Tesla.Web.Pages
{
    public partial class PhaseDetails : PageBase
    {
        #region Class Level Variables
        private PhaseServiceDispatcher phaseDispatcher = null;
        private int? PhaseRecordCount = 0;
        #endregion


        protected void Page_Load(object sender, EventArgs e)
        {
            List<PhaseDC> phase = null; IList<BaseDC> projects = null;

            phaseDispatcher = new PhaseServiceDispatcher();

            try
            {
                if (!IsPostBack)
                {
                    if (Request.QueryString["ID"] != null)
                    {
                        phase = phaseDispatcher.ReadPhase(Convert.ToInt32(Request.QueryString["ID"])).Cast<PhaseDC>().ToList();
                        SetValuesForControls(phase);
                        Legend1.InnerText = "Update Phase";
                    }

                    projects = phaseDispatcher.ReadAllProjects().Cast<BaseDC>().ToList();

                    ArrayList arrProjects = new ArrayList();

                    if (projects != null && projects.Count > 0)
                    {
                        foreach (BaseDC pDC in projects)
                        {
                            arrProjects.Add((ProjectDC)pDC);
                        }
                    }

                    lstProjects.DataValueField = "ProjectID"; lstProjects.DataTextField = "ProjectCode";
                    this.lstProjects.DataSource = arrProjects;
                    this.lstProjects.DataBind();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                int? phaseID = 0, success = 0;
                PhaseDC createdPhase = new PhaseDC();
                createdPhase.PhaseCode = txtPhaseCode.Text;
                createdPhase.PhaseShortDescription = txtPhaseShortDescription.Text;
                createdPhase.PhaseCompletionPct = Convert.ToDouble(txtPhaseCompletionPct.Text);
                createdPhase.ProjectID = Convert.ToInt32(lstProjects.SelectedItem.Value);
                phaseDispatcher = new PhaseServiceDispatcher();

                if (ViewState["SelectedPhaseId"] == null)
                {
                    phaseID = phaseDispatcher.CreatePhase(createdPhase);
                }
                else
                {
                    createdPhase.PhaseID = Convert.ToInt32(ViewState["SelectedPhaseId"]);
                    success = phaseDispatcher.UpdatePhase(createdPhase);
                }
                if (!Page.ClientScript.IsClientScriptBlockRegistered("saveAlert"))
                {
                    Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "saveAlert", "alert('Data has been succesfully saved '); window.location.href('PhaseSearch.aspx')", true);
                }


            }

            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void SetValuesForControls(List<PhaseDC> phase)
        {
            ViewState["SelectedPhaseId"] = ((PhaseDC)phase[0]).PhaseID;
            this.txtPhaseCode.Text = ((PhaseDC)phase[0]).PhaseCode;
            this.txtPhaseShortDescription.Text = ((PhaseDC)phase[0]).PhaseShortDescription;
            this.txtPhaseCompletionPct.Text = ((PhaseDC)phase[0]).PhaseCompletionPct.ToString();
            this.lstProjects.SelectedValue = ((PhaseDC)phase[0]).ProjectID.ToString();
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            try
            {
                Response.Redirect(VirtualPathUtility.ToAbsolute("~/") + @"Pages/Administrator/PhaseSearch.aspx", false);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
